import request from "@/utils/request";
import { uaa_server } from "/src/constants/serverName";
import cache from "/src/plugins/cache";
import { FORM_URLENCODED } from "/src/constants/contentTypeEnum";
import JSEncrypt from "jsencrypt";

// 登录方法
export function login(username, password, code, codeKey) {
  const formData = new FormData();
  //公钥
  const publicKey = import.meta.env.VITE_APP_PUBLIC_KEY;
  const crypt = new JSEncrypt();
  crypt.setPublicKey(publicKey);
  formData.append("username", username);
  formData.append("password", crypt.encrypt(password));
  formData.append("code", code);
  formData.append("codeKey", cache.local.get("codeKey"));

  return request({
    url: uaa_server + "/user/login",
    headers: {
      isToken: false,
      repeatSubmit: false,
      accountType: "admin",
      "Content-Type": FORM_URLENCODED,
    },
    method: "post",
    data: formData,
  });
}

// 注册方法
export function register(data) {
  return request({
    url: uaa_server + "/user/register",
    headers: {
      isToken: false,
    },
    method: "post",
    data: data,
  });
}

// 获取用户详细信息
export function getInfo() {
  return request({
    url: uaa_server + "/user/getInfo",
    method: "get",
  });
}

// 退出方法
export function logout() {
  return request({
    url: uaa_server + "/user/logout",
    method: "post",
  });
}

// 获取验证码
export function getCodeImg() {
  return request({
    url: uaa_server + "/validate/code/captchaImage",
    headers: {
      isToken: false,
    },
    method: "get",
    timeout: 20000,
  });
}
